package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

public class JDBCDemo8 {
    public static void main(String[] args) {
        Scanner is = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String id = is.nextLine();
        System.out.println("请输入姓名：");
        String name = is.nextLine();
        System.out.println("请输入密码：");
        String password = is.nextLine();
        System.out.println("请输入地址：");
        String address = is.nextLine();
        try (Connection conn = DBUtil.getConnection();) {
            String sql="INSERT INTO user( id,name,password,address)"+
                    "VALUES(?,?,?,?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            stmt.setString(2, name);
            stmt.setString(3, password);
            stmt.setString(4, address);
            int count = stmt.executeUpdate();
            if (count>0) {
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
